<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas</title>
    <style type="text/css">
        body {
            text-align: center;
            padding-top: 20px;
        }
        canvas {
            box-shadow: 0 0 10px #333;
            margin: 0 auto;
        }
    </style>
</head>
<body onload="draw()">
<canvas id="canvas" width="800" height="600">
    你的浏览器太 low 了，请更新~!
</canvas>

<script>
    function draw() {
        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            var ctx = canvas.getContext('2d');

            // 添加鼠标按下事件
            canvas.onmousedown = function (e) {
                var ev = e || window.event;
                var x = ev.clientX - canvas.offsetLeft;
                var y = ev.clientY - canvas.offsetTop;

                ctx.strokeStyle = 'green';
                ctx.lineWidth = 10;

                ctx.beginPath();
                ctx.moveTo(x, y);

                // onmousemove
                canvas.onmousemove = function (e) {
                    var ev = e || window.event;
                    var x = ev.clientX - canvas.offsetLeft;
                    var y = ev.clientY - canvas.offsetTop;
                    ctx.lineTo(x, y);
                    ctx.stroke();
                };

                // onmourseup
                canvas.onmouseup = function (e) {
                    canvas.onmousemove = '';
                }
            }
        }
    }
</script>
</body>
</html>
